﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NetworkCrashSimulator
{

    public partial class MainForm : Form
    {
        private Core.NetworkAdapterConfiguration SelectedNetworkAdapterConfiguration { get; set; }

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            GetNetworkAdapters();
        }

        private void GetNetworkAdapters()
        {
            foreach (Core.NetworkAdapterConfiguration networkAdapterConfiguration in Core.Actions.ListAdapters())
            {
                cbxNetworkAdapters.Items.Add(new Generic.ComboItem(networkAdapterConfiguration, networkAdapterConfiguration.Description));
            }
        }

        private void GetNetworkAdapterSettings()
        {
            lblMacAddressValue.Text = SelectedNetworkAdapterConfiguration.MACAddress;
            if (SelectedNetworkAdapterConfiguration.DHCPEnabled)
            {
                lblDhcpEnabledValue.Text = "Yes";
            }
            else
            {
                lblDhcpEnabledValue.Text = "No";
            }
            if (SelectedNetworkAdapterConfiguration.IPEnabled)
            {
                lblCrasheable.Text = "This network adapter can be crashed.";
                lblCrasheable.ForeColor = Color.Green;
            }
            else
            {
                lblCrasheable.Text = "This network adapter can not be crashed.";
                lblCrasheable.ForeColor = Color.Red;
            }
        }

        private void SetSelectedNetworkAdapterSettings(object sender, EventArgs e)
        {
            Generic.ComboItem comboItem = (Generic.ComboItem)cbxNetworkAdapters.SelectedItem;
            SelectedNetworkAdapterConfiguration = (Core.NetworkAdapterConfiguration)comboItem.BindingValue;
            GetNetworkAdapterSettings();
        }

        private void CrashNetworkAdapter(object sender, EventArgs e)
        {
            if (ValidateNetworkAdapter())
            {
                if (Generic.Message.ConfirmAction("Do you really want to crash the selected network adapter?"))
                {
                    Core.Actions.CrashAdapter(SelectedNetworkAdapterConfiguration.Description);
                }
            }
        }

        private void RepairNetworkAdapter(object sender, EventArgs e)
        {
            if (ValidateNetworkAdapter())
            {
                Core.Actions.RepairAdapter(SelectedNetworkAdapterConfiguration.Description);
            }
        }

        private void ValidadeNuds(object sender, EventArgs e)
        {
            //TODO: Add validation to the NUDs (NumericUpDowns)
        }

        private void StartTimmedCrash(object sender, EventArgs e)
        {
            if (ValidateNetworkAdapter())
            {
                if (Generic.Message.ConfirmAction("Do you really want to start timmed crashes on the selected network adapter?"))
                {
                    cbxNetworkAdapters.Enabled = false;
                    btnManuallyCrashNetworkAdapter.Enabled = false;
                    btnManuallyRepairNetworkAdapter.Enabled = false;
                    nudTimesToCrash.Enabled = false;
                    nudTimeToRecover.Enabled = false;
                    nudTotalTimeRun.Enabled = false;
                    btnStopTimmedCrashes.Enabled = true;
                    btnStartTimmedCrashes.Enabled = false;
                    StartProgressBar();
                    bgwTimmedCrashes.RunWorkerAsync();
                }
            }
        }

        private bool ValidateNetworkAdapter()
        {
            bool ret = true;

            if (cbxNetworkAdapters.SelectedIndex < 0)
            {
                Generic.Message.ShowErrorMessage("No network adapter is selected.");
                ret = false;
            }
            else
            {
                if (!SelectedNetworkAdapterConfiguration.IPEnabled)
                {
                    Generic.Message.ShowErrorMessage("This network adapter can not be crashed.");
                    ret = false;
                }
            }
            return ret;
        }

        private void StopTimmedCrash(object sender, EventArgs e)
        {
            if (Generic.Message.ConfirmAction("Do you really want to stop the timmed crashes?/nIf your adapter is crashed you have to manually repair it."))
            {
                bgwTimmedCrashes.CancelAsync();
            }
        }

        private void RunTimmedCrashes(object sender, DoWorkEventArgs e)
        {
            Core.Actions.TimedCrash(SelectedNetworkAdapterConfiguration.Description, (int)nudTotalTimeRun.Value, (int)nudTimesToCrash.Value, (int)nudTimeToRecover.Value);
        }

        private void TimmedCrashesStoped(object sender, RunWorkerCompletedEventArgs e)
        {
            StopProgressBar();
            Generic.Message.ShowInformationMessage("Timmed crash has ended.");
            cbxNetworkAdapters.Enabled = true;
            btnManuallyCrashNetworkAdapter.Enabled = true;
            btnManuallyRepairNetworkAdapter.Enabled = true;
            nudTimesToCrash.Enabled = true;
            nudTimeToRecover.Enabled = true;
            nudTotalTimeRun.Enabled = true;
            btnStopTimmedCrashes.Enabled = false;
            btnStartTimmedCrashes.Enabled = true;
        }

        internal void StartProgressBar()
        {
            pgbTimmedRunning.Style = ProgressBarStyle.Marquee;
        }

        internal void StopProgressBar()
        {
            pgbTimmedRunning.Style = ProgressBarStyle.Blocks;
            pgbTimmedRunning.Value = 0;
        }
    }
}
